#!/bin/bash
###########################################################################################
#
#THIS SCRIPT WRITTEN FOR CARB, AQSB, OSS.
#THIS SCRIPT WORKS WITH MYSQL TO MAKE DATA FOR DMS INGEST.
#FOR QUESTIONS REGARDING THE OUTPUT OF THE FIELD, SEE DOCUMENTATION
#ON DMSOUT.CFG. IF YOU LACK MYSQL OR THE CL_INGESTS DATABASE, THIS SCRIPT WILL INSTALL
#IT FOR YOU. IT RELIES ON:
#	A) sys_resources/dms.cfg
#   B) the .display file in each driver.
#	C) the variables contained in Config/config
#--DECEMBER 5, 2011; MATT VONA
#
###########################################################################################
errors='';
if [ $# -lt 2 ]; then
	{
		echo "Proper use: $0 [Begining Date] [Ending Date]";
		exit
	}
fi
if !( touch "sys_resources/dms.cfg" ); then
	{
	
		echo "Must have sys_resources/dms.cfg present!!!";
		exit
	
	}
fi
#GET SYSTEM WIDE VARIABLES
#read config file
while read line; do
eval $line
done < Config/config
#MAKE SURE MYSQL IS INSTALLED AND THAT THE REQUIRED DATABASE IS PRESENT
if !( which mysql ); then
{
echo "No mysql installed here. Would you like me to install it? (Y or N)";
read response;
	if [ $response == "Y" ]; then
		{
		bash sys_resources/mysql.install;
		wait;
		}
	else
		{
		echo "Nothing left to do;"
		exit;
		}
	fi	
}
else
{
	aminstalled=`mysql -u root --password=carblogger -e "DESCRIBE cl_ingests;" carblogger | wc -l`
	if [ $aminstalled -lt 3 ]; then
		{
		echo "Hmm...lights are on but no-one is home. Want me to set up a carblogger database for you?";
		read response;
		if [ $response=="Y" ]; then
			{
				echo "Installing the ingest tables";
				mysql -u root --password=carblogger < sys_resources/alldb_backup.sql				
			}
		fi
		}
	fi
}
fi
#CREATE VARIABLES FOR THE NAMES OF THE HIDDEN FILES TO BE USED
tmpfile=sys_resources/.${$}dmsprcssng 
tmpcfg=sys_resources/.${$}dmscfg
###########################################################################
#
#THIS SECTION BEGINS BUILDING A TEXT FILE WHICH WILL BE DIRECTED INTO THE MYSQL DATABASE.
#
##########################################################################
echo 'use carblogger;' > $tmpfile
#counter=0
#FIRST WE POPULATE THE PARAMETER FIELD USING THE CONFIG FILE LOCATED
#IN THE sys_resources/dms.cfg file. First step is to filter out all commented lines
# and place the new file into a hidden .tmpcfg file.
cat "sys_resources/dms.cfg" | grep -v '#' | grep -v "^$" > $tmpcfg 		
#CREATE A GLOBAL SQL VARIABLE TO COMMUNICATE THE END DATE TO MYSQL
echo 'set @e_date="'$2'";' >> $tmpfile ;
#CREATE A GLOBAL SQL VARIABLE TO COMMUNICATE THE BEGINING DATE TO MYSQL.
echo 'set @b_date="'$1'";' >> $tmpfile ;
#THE .MIN FILES MUST USE THE SAROAD NUMBER FOR THE SITE< THIS IS IN THE CONFIG/CONFIG FILE
echo 'set @saroad="'$saroad'";' >> $tmpfile ;
#CALL THE CARB_CALS STORED PROCEDURE WHICH PREPARES A TEMPORARY TABLE
#TEMPORARY TABLE TO STORE THE CONFIGURATION FOR THE SITE.
echo 'call CARB_CALS();' >> $tmpfile ;
#Once the temp config file has been parsed, update the parameter field
#in cl_ingests to the DMS label for each respective data type.
		( while read line 
			do
			{
				#grab the label name
				this_label=`echo $line | awk '{ print $1 }'`
				#grab the parameter
				this_parameter=`echo $line | awk '{ print $2 }'`
				#grab conditions matching zero for this label
				this_zero=`echo $line | awk '{ print $3 }'`
				#grab conditions matching span for this channel
				this_span=`echo $line | awk '{ print $4 }'`
				#grab conditions matching precision for this channel
				this_prec=`echo $line | awk '{ print $5 }'`
				#grab conditions matching offset minutes for this channel
				this_offset=`echo $line | awk '{ print $6 }'`
				#grab the system of this instrument
				this_system=`echo $line | awk '{ print $7 }'`
				
				#POPULATE THE CONFIGS TABLE GENERATED BY the call CARB_CALS(); 					
				echo 'INSERT INTO `carblogger`.`Cfgs` (data_label, parameter, prec, span, zero, offset, system) VALUES("'$this_label'","'$this_parameter'","'$this_prec'","'$this_span'","'$this_zero'","'$this_offset'","'$this_system'");'>>$tmpfile		
			}
			done

		)<$tmpcfg
#CALL THE CALS_OUT stored procedure.
		echo 'call CALS_OUT();' >> $tmpfile ;
#FINISH THE FILE FOR INGEST INTO MYSQL
		echo "quit" >> $tmpfile        
PASS=carblogger
/usr/bin/mysql -u root --password=$PASS  < $tmpfile &>.tmpout 
#CREATE THE DATE STRING NEEDED BY DMS FOR THE FILE NAME
datestring=`date +%b_%d_%Y_%T`
datestring=`echo $datestring | sed 's/:/_/g'`
#THIS NEXT STATEMENT SIMPLY XHANGES THE MQSQL DUMP FORMAT INTO the PIPE DELIMITED FORMAT 
#NEEDED BY DMS
cat .tmpout | grep -v ^SITE | grep -v ^saroad | sed 's/\t/"|"/g;s/\n//g;s/$/|/g;s/"//g;s/$/\r/g' > ${saroad}-${datestring}.MIN 
#uncomment the following lines to debug
#gedit $tmpfile;
rm -f $tmpcfg;
#gedit .tmpout;
rm -f $tmpfile;
rm -f .tmpout;
exit;
